class Media < ActiveRecord::Base
  belongs_to :category
  belongs_to :user
  has_many :comments

  validates_presence_of :nom

  def self.per_page
    20
  end

  def url=(link)
    if wrappers = WebWrapper.find(:all)
      wrappers.each do |wrapper|
        if link =~ Regexp.new(wrapper.url_regex)
          if imgsrc = wrapper.wrap_source_url(link)
            unless imgsrc.empty?
              self.img_src = imgsrc
              puts "Image src of #{self.nom} wraped with #{wrapper.url_regex} : #{self.img_src}" if defined?DEBUG
              break
            end
          end
        end
      end
    end
    write_attribute(:url, link)
  end

  def local_path=(lp)
    if (lp && !lp.match(/\.\.\//) && !lp.match(/\/\.\./) && !lp.match(/^\.\.$/) && !lp.match(/^\.$/))
      write_attribute(:local_path, lp)
    else
      write_attribute(:local_path, nil)
    end
  end
end
